/* Editor.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the Editor class, which stores all the TaskLists and contains
the functionality for editing them, including all responses to key events.
*/

#ifndef __MZ_EDITOR_H__
#define __MZ_EDITOR_H__

#include "_common/Noncopyable.h"

#include "CategoryList.h"
#include "TaskList.h"
#include "Task.h"
#include "Selection.h"

#include <SDL_keysym.h>

#include <string>

using std::string;



class Editor : public Noncopyable {
public:
	// Constructor.
	Editor();
	
	// Load the data file from the given directory.
	bool Initialize(const string &dataFilePath);
	bool InitializeFromData(const string &data);
	// Get the path to the file we're editing.
	const string &FilePath() const;
	// Save the data file.
	void Save();
	// Revert to the saved version of the data.
	void Revert();
	// Load all categories that have been deleted and append them
	// to the currently open set of categories.
	void Resurrect();
	
	// Mouse event handlers.
	void SelectCategory(int category);
	// Assume the calling function checks that the row is valid,
	// since it will have to determine where in the row the click occurred anyways.
	void ToggleExpansion(int row, bool recursive);
	void ToggleCheck(int row, bool recursive);
	void SelectText(int row);
	void SelectDate(int row);
	void ExpandSelection(int row);
	
	// Handle the given key event. Returns false if the key is not a recognized command.
	bool HandleKey(SDLKey key, SDLMod mod);
	
	// Determine what needs to be drawn.
	bool IsEditingCategory() const { return editMode == EDIT_CATEGORY; }
	bool IsEditingRow() const { return IsEditingText() | IsEditingDate(); }
	bool IsEditingText() const { return (editMode == EDIT_TEXT); }
	bool IsEditingDate() const { return (editMode == EDIT_DATE); }
	bool IsSearching() const { return (editMode == EDIT_SEARCH); }
	
	// Get the category list.
	const CategoryList &Categories() const { return categories; }
	// Get the current task list.
	const TaskList &Tasks() const { return *tasks; }
	
	// For editing the preferences file: create or select this category.
	TaskList &GetCategory(const string &name);
	// After hand-modifying that category, it must be Update()d.
	void Update(const string &name, list<Task> &newList);
	
	// String that the user is currently entering.
	const string &EditString() const { return editString; }
	// Start and end character indices for the cursor selection.
	Selection TextSelection() const { return textSelection; }
	// Row that is actively being edited.
	int EditRow() const { return editRow; }
	const Task &EditTask() const { return categories.Selected()[editRow]; }
	
	// Set the number of rows per page. This depends on the display size.
	void SetPageSize(int rows) { PAGE_ROWS = rows; }
	
	
private:
	// These are the data fields that can be edited.
	enum Mode {
		EDIT_NOTHING,
		EDIT_SEARCH,
		EDIT_TEXT,
		EDIT_DATE,
		EDIT_CATEGORY
	};


private:
	// Enter search mode.
	void BeginSearch(bool anywhereInLine);
	
	// Do text editing based on the given keypress. Return false if the key event
	// should be handled by the main handler as well.
	bool HandleText(SDLKey key, SDLMod mod);
	// Change to the given editing mode.
	void ChangeEditMode(Mode newMode, int newRow = -1);

	// Search for text. Return the first matching row.
	int Search(int firstRow, int endRow);


private:
	// The list of categories, and the current category.
	CategoryList categories;
	CategoryList::iterator &tasks;

	// Editing mode. If editing, editString will store the string that the user is
	// entering.
	Mode editMode;
	string editString;
	// If the mode is EDIT_SEARCH, this flag controls whether we search in all the text
	// or just the beginning of each line.
	bool searchAnywhere;
	// Begin the search at this line. This allows the user to tab between search hits.
	int searchBeginRow;
	// The characters of text that are selected. This is valid only if editing.
	Selection textSelection;
	int editRow;
	
	// The number of rows to move on page up / down.
	int PAGE_ROWS;


private:
	// For handling shift + character.
	static const char SHIFT[95];
};



#endif

/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
